import React from 'react';
import { Button } from '@teamix/ui';
import PropTypes from 'prop-types';
import ListEmpty from 'atComponents/ListEmpty';
import LogModal from 'components/compileLogModal';
import track from 'utils/track';
import intl from '../../../../../../../../../locale';

export default class WarningPage extends React.Component {
  static propTypes = {
    buildResult: PropTypes.object,
  };

  static defaultProps = {
    buildResult: null,
  };

  state = {
    logVisible: false,
  };

  handleManual = () => {
    this.props.startScan();
    track({
      action: 'manual_trigger_scan',
      page: 'Security-Sensitive-Info-Detection',
      type: '',
      control: '',
    });
  };

  showLog = () => {
    this.setState({
      logVisible: true,
    });
  };

  render() {
    const { buildResult, isStarting, incompleteData, issueCount } = this.props;
    const { logVisible } = this.state;
    if (!buildResult && !incompleteData) {
      return (
        <ListEmpty
          type="project"
          className="no-content"
          content=""
          description={intl.get({
            id: 'code-assets.dependencyAnalysis.containers.warningPage.TheCurrentBranchHasNot',
            defaultMessage: '当前分支暂未执行依赖包漏洞检测',
          })}
          imgType="search"
          btns={[
            <Button
              type="primary"
              loading={isStarting}
              style={{ width: 88 }}
              onClick={this.handleManual}
            >
              {intl.get({
                id: 'code-assets.dependencyAnalysis.containers.warningPage.ScanNow',
                defaultMessage: '立即扫描',
              })}
            </Button>,
          ]}
        />
      );
    }

    // count为0， 并且buidlResult.id < incompleteData.id incompleteData结果为准 ,否则buildResult优先
    const isInCompleteDataFirst =
      buildResult &&
      incompleteData &&
      buildResult.id &&
      incompleteData.id &&
      buildResult.id < incompleteData.id &&
      (issueCount === 0 ||
        incompleteData.state.toLowerCase() === 'running' ||
        incompleteData.state.toLowerCase() === 'error');
    // 都有数据显示buildResult优先
    if (!isInCompleteDataFirst && buildResult && buildResult.state.toLowerCase() === 'pass') {
      return (
        <ListEmpty
          type="project"
          className="no-btns"
          content={intl.get({
            id: 'code-assets.dependencyAnalysis.containers.warningPage.NoDependencyPackageVulnerability',
            defaultMessage: '暂无依赖包漏洞问题',
          })}
          description={intl.get({
            id: 'code-assets.dependencyAnalysis.containers.warningPage.GreatKeepTryingToKeep',
            defaultMessage: '太棒了，继续努力保持哦',
          })}
          imgType="500"
        />
      );
    }
    // fail && warning  && count为0,表示有旧数据
    if (
      !isInCompleteDataFirst &&
      buildResult &&
      (buildResult.state.toLowerCase() === 'fail' || buildResult.state.toLowerCase() === 'warning')
    ) {
      return (
        <ListEmpty
          type="project"
          className="no-content"
          content=""
          description={intl.get({
            id: 'code-assets.dependencyAnalysis.containers.warningPage.PerformAScanToObtain',
            defaultMessage: '执行扫描以获取当前分支的最新检测结果',
          })}
          image="https://img.alicdn.com/tfs/TB1jAyCzAT2gK0jSZFkXXcIQFXa-600-600.png"
          btns={[
            <Button type="primary" loading={isStarting} onClick={this.handleManual}>
              {intl.get({
                id: 'code-assets.dependencyAnalysis.containers.warningPage.ScanNow',
                defaultMessage: '立即扫描',
              })}
            </Button>,
          ]}
        />
      );
    }

    if (incompleteData && incompleteData.state.toLowerCase() === 'error') {
      return (
        <>
          {logVisible && (
            <LogModal
              isModalOpen={logVisible}
              projectId={window.FORCE.pathResource.id}
              build={incompleteData}
              closeModal={() => {
                this.setState({
                  logVisible: false,
                });
              }}
            />
          )}
          <ListEmpty
            type="project"
            className="no-content"
            content=""
            description={intl.get({
              id: 'code-assets.dependencyAnalysis.containers.warningPage.AnErrorOccurredWhileDetecting',
              defaultMessage: '依赖包漏洞检测执行异常',
            })}
            imgType="search"
            btns={[
              <Button type="primary" onClick={this.showLog}>
                查看日志
              </Button>,
              <Button type="primary" loading={isStarting} onClick={this.props.restartScan}>
                {intl.get({
                  id: 'code-assets.dependencyAnalysis.containers.warningPage.TryAgain',
                  defaultMessage: '重试',
                })}
              </Button>,
            ]}
          />
        </>
      );
    }
    if (
      incompleteData &&
      (incompleteData.state.toLowerCase() === 'running' ||
        incompleteData.state.toLowerCase() === 'waiting')
    ) {
      return (
        <ListEmpty
          type="project"
          className="no-content no-btns "
          content=""
          description={intl.get({
            id: 'code-assets.dependencyAnalysis.containers.warningPage.DependencyPackageVulnerabilityDetectionAnd',
            defaultMessage: '依赖包漏洞检测扫描中…',
          })}
          imgType="search"
        />
      );
    }
    return null;
  }
}
